package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.AllItemResponse;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HomepageShoppingController
 * @Description: Shopping前四个接口控制层
 * @Author:YPS
 * @Date: 2022/5/19 12:11
 * @Version: 1.0
 **/

@RestController
public class HomepageShoppingController {

    @Autowired
    IHomeService homeService;
    @Autowired
    IContentService contentService;
    @Autowired
    ICategoriesService categoriesService;
    @Autowired
    IProductService productService;



    @GetMapping("shopping/homepage")
    public ResponseData<HomePageResponse> getHomepage(){
        HomePageResponse response = homeService.homepage();
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setData(response.getPanelContentItemDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("shopping/navigation")
    public ResponseData<NavListResponse> getNavigation(){
        NavListResponse response = contentService.queryNavList();
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getPannelContentDtos());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("shopping/categories")
    public ResponseData<CategoriesResponse> getCategories(){
        CategoriesResponse response = categoriesService.showAllProducts();
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductCateDtoList());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }
    @GetMapping("shopping/product/{id}")
    public ResponseData<ProductDetailResponse> getProductDetails(@PathVariable("id")Integer id, HttpServletRequest request){
        ProductDetailRequest productDetailRequest = new ProductDetailRequest();
        productDetailRequest.setId(Long.valueOf(id));
        ProductDetailResponse response = productService.getProductDetail(productDetailRequest);
        System.out.println(response.getCode());
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil().setData(response.getProductDetailDto());
        }
        return new ResponseUtil().setErrorMsg(response.getMsg());
    }

    @GetMapping("shopping/goods")
    /**
     * @author: huzhouming
     * @description: TODO 分页查询商品信息
     * @date: 2022/5/20
     * @param request
     * @return com.cskaoyan.mall.commons.result.ResponseData<com.cskaoyan.shopping.dto.AllProductResponse>
     */
    public ResponseData<AllProductResponse> getGoods(AllProductRequest request){
        AllProductResponse response = productService.getAllProduct(request);
        Map<String, Object> map = new HashMap<>();
        map.put("data",response.getProductDtoList());
        map.put("total",response.getTotal());
        if(response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setData(map);
       }
        return new ResponseUtil().setErrorMsg(response.getMsg());
   }

   @GetMapping("shopping/recommend")
   /**
    * @author: huzhouming
    * @description: TODO 查询推荐商品
    * @date: 2022/5/20
    * @param
    * @return com.cskaoyan.mall.commons.result.ResponseData
    */
    public ResponseData recommend(){
       RecommendResponse response = productService.getRecommendGoods();
       if(response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
           return new ResponseUtil().setData(response.getPanelContentItemDtos());
       }
       return new ResponseUtil().setErrorMsg(response.getMsg());
   }

//   @PostMapping("shopping/search")
//   /**
//    * @author: huzhouming
//    * @description: TODO 模糊查询商品名称
//    * @date: 2022/5/20
//    * @param
//    * @return com.cskaoyan.mall.commons.result.ResponseData
//    */
//    public ResponseData search(@RequestBody SearchProductRequest request){
//       AllProductResponse response = productService.search(request);
//       Map<String, Object> map = new HashMap<>();
//       map.put("data",response.getProductDtoList());
//       map.put("total",response.getTotal());
//       if(response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
//           return new ResponseUtil().setData(map);
//       }
//       return new ResponseUtil().setErrorMsg(response.getMsg());
//   }
}
